package zhoug.chart.core.styleconfig;

import com.github.mikephil.charting.model.GradientColor;
import zhoug.chart.MPChartColors;
import zhoug.chart.MPChartConfig;
import zhoug.chart.utils.MPChartUtils;

import java.util.List;

/**
 * @Author: zhoug
 * @Date: 2024-02-04
 * @Description:
 */
public class MPBarStyleConfig extends MPStyleConfig implements IMPBarStyleConfig {
    private int[] barColors;//柱状图颜色
    private List<GradientColor> gradientColors;//柱状图渐变颜色,优先级高于mBarColors
    private boolean stackBar;//柱状图是否堆叠 mpBarLineKeys属性为null时生效
    private boolean overLap;//柱状图是否重叠
    private float barWidth = MPChartConfig.DEFAULT_BAR_WIDTH;//一组只有1个柱子时的柱子宽度 0.0f-1.0f
    private float groupSpace = 1.0f;//组之前的间隔占柱子宽度的比例
    private float barSpace = 0.1f;//柱子之前的间隔占柱子宽度的比例
    /**
     * true:groupSpace和barSpace值表示宽度的百分比
     * false:groupSpace和barSpace值表示原生值
     */
    private boolean spaceIsPercent = true;


    @Override
    public void setBarColors(int[] barColors) {
        this.barColors = barColors;
    }


    @Override
    public void setGradientColors(List<GradientColor> gradientColors) {
        this.gradientColors = gradientColors;
    }

    @Override
    public void setStackBar(boolean stackBar) {
        this.stackBar = stackBar;
    }

    /**
     * 重叠
     *
     * @param overlap
     */
    @Override
    public void setOverlap(boolean overlap) {
        this.overLap=overlap;
    }

    /**
     * 重叠
     */
    @Override
    public boolean isOverlap() {
        return this.overLap;
    }

    @Override
    public void setBarWidth(float barWidth) {
        if (barWidth <= 0 || barWidth > 1) {
            this.barWidth = MPChartConfig.DEFAULT_BAR_WIDTH;
        } else {
            this.barWidth = barWidth;
        }
    }

    @Override
    public void setGroupSpace(float groupSpace) {
        if (groupSpace < 0) {
            this.groupSpace = 0;
        } else {
            this.groupSpace = groupSpace;
        }
    }

    @Override
    public void setBarSpace(float barSpace) {
        if (barSpace < 0) {
            this.barSpace = 0;
        } else {
            this.barSpace = barSpace;
        }
    }


    @Override
    public void setSpaceIsPercent(boolean spaceIsPercent) {
        this.spaceIsPercent = spaceIsPercent;
    }


    @Override
    public int[] getBarColors() {
        if (barColors == null || barColors.length == 0) {
            barColors = MPChartColors.COLORS;
        }
        return barColors;
    }


    @Override
    public List<GradientColor> getGradientColors() {
        return gradientColors;
    }

    @Override
    public boolean isStackBar() {
        return stackBar;
    }

    @Override
    public float getBarWidth() {
        return barWidth;
    }

    @Override
    public float getGroupSpace() {
        return groupSpace;
    }

    @Override
    public float getBarSpace() {
        return barSpace;
    }


    @Override
    public boolean isSpaceIsPercent() {
        return spaceIsPercent;
    }


    @Override
    public int getBarColor(int index) {
        return MPChartUtils.getColorForIndex(getBarColors(), index);
    }

    @Override
    public GradientColor getBarGradientColor(int index) {
        return MPChartUtils.getGradientColorForIndex(getGradientColors(), index);
    }


}
